// i.d(t, { D: () => LabelData })
import Data from "../object/Data"
import ObservableMap, { createMap } from "../observer/observable.map"
import funcUtil from "../utils/func.util"
import Logger from "../utils/Logger"
import Label from "../object/Label"
const o = new Logger("label-data")
export default class LabelData extends Data {
  map: ObservableMap<Label>
  constructor(e?: Label) {
    super()
    this.name = "label"
    this.map = createMap(e || {})
  }
  getLabel(e: string) {
    return this.map.get(e)
  }
  has(e: string) {
    return this.map.has(e)
  }
  getCount() {
    return this.map.length
  }
  add(e: Label) {
    let t = e.sid
    this.map.has(t) &&
      o.warn("Label already tracked:", {
        sid: t,
        label: e,
        existing: this.getLabel(t)
      })
    t = t || this.generateSid()
    e.sid = t
    e.commit()
    this.map.set(e.sid, e)
    return t
  }
  generateSid() {
    let e = ""
    for (; !e || this.map.has(e); ) e = funcUtil.randomStr(11)
    return e
  }
  remove(e: string) {
    return !!this.map.has(e) && (this.map.delete(e), !0)
  }
  clear() {
    for (const e of this.map.keys) this.map.delete(e)
  }
  getCollection() {
    return this.map
  }
  iterate(e: (v: Label, key: string) => void) {
    for (const t of this.map.keys) e(this.map.get(t), t)
  }
  isEqual(e: LabelData) {
    if (this.map.length !== e.map.length) return !1
    for (const t of this.map) {
      const i = e.map.get(t.sid)
      if (!i || !LabelData.labelsAreEqual(t, i)) return !1
    }
    return !0
  }
  static labelsAreEqual(e: Label, t: Label) {
    return (
      e.floorId === t.floorId &&
      e.roomId === t.roomId &&
      e.position.x === t.position.x &&
      e.position.y === t.position.y &&
      e.position.z === t.position.z &&
      e.sid === t.sid &&
      e.text === t.text &&
      e.floorId === t.floorId &&
      e.roomId === t.roomId &&
      e.visible === t.visible
    )
  }
  getLabelsForRoom(roomId: string) {
    const t = []
    this.iterate(i => {
      i.roomId === roomId && t.push(i)
    })
    return t
  }
}
